<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="企业名称" prop="enterpriseName">
        <el-input v-model="formData.enterpriseName" placeholder="请输入企业名称" />
      </el-form-item>
      <el-form-item label="注册地址" prop="registeredAddress">
        <el-input v-model="formData.registeredAddress" placeholder="请输入注册地址" />
      </el-form-item>
      <el-form-item label="注册经营范围" prop="businessScope">
        <el-input v-model="formData.businessScope" placeholder="请输入注册经营范围" />
      </el-form-item>
      <el-form-item label="统一社会信用代码" prop="unifiedSocialCreditCode">
        <el-input v-model="formData.unifiedSocialCreditCode" placeholder="请输入统一社会信用代码" />
      </el-form-item>
      <el-form-item label="法定代表人姓名" prop="legalRepresentative">
        <el-input v-model="formData.legalRepresentative" placeholder="请输入法定代表人姓名" />
      </el-form-item>
      <el-form-item label="注册登记机关" prop="registrationAuthority">
        <el-input v-model="formData.registrationAuthority" placeholder="请输入注册登记机关" />
      </el-form-item>
      <el-form-item label="机构性质" prop="institutionNature">
        <el-input v-model="formData.institutionNature" placeholder="请输入机构性质" />
      </el-form-item>
      <el-form-item label="国有企业类型" prop="soeType">
        <el-select v-model="formData.soeType" placeholder="请选择国有企业类型">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="申请备案名" prop="applicationName">
        <el-input v-model="formData.applicationName" placeholder="请输入申请备案名" />
      </el-form-item>
      <el-form-item label="人社部机构备案号" prop="mohrssFilingNumber">
        <el-input v-model="formData.mohrssFilingNumber" placeholder="请输入人社部机构备案号" />
      </el-form-item>
      <el-form-item label="总公司名称" prop="headOfficeName">
        <el-input v-model="formData.headOfficeName" placeholder="请输入总公司名称" />
      </el-form-item>
      <el-form-item label="上年度企业在职技能人员总人数" prop="lastYearEmployeeCount">
        <el-input v-model="formData.lastYearEmployeeCount" placeholder="请输入上年度企业在职技能人员总人数" />
      </el-form-item>
      <el-form-item label="上年度企业教育经费用于技能人才培养额度" prop="lastYearEducationFund">
        <el-input v-model="formData.lastYearEducationFund" placeholder="请输入上年度企业教育经费用于技能人才培养额度" />
      </el-form-item>
      <el-form-item label="从业人数" prop="employeeCount">
        <el-input v-model="formData.employeeCount" placeholder="请输入从业人数" />
      </el-form-item>
      <el-form-item label="注册资本（万元）" prop="registeredCapital">
        <el-input v-model="formData.registeredCapital" placeholder="请输入注册资本（万元）" />
      </el-form-item>
      <el-form-item label="上年度营业收入（万元）" prop="lastYearRevenue">
        <el-input v-model="formData.lastYearRevenue" placeholder="请输入上年度营业收入（万元）" />
      </el-form-item>
      <el-form-item label="评价业务负责人" prop="evaluationBusinessLeader">
        <el-input v-model="formData.evaluationBusinessLeader" placeholder="请输入评价业务负责人" />
      </el-form-item>
      <el-form-item label="评价业务负责人手机号" prop="evaluationBusinessLeaderPhone">
        <el-input v-model="formData.evaluationBusinessLeaderPhone" placeholder="请输入评价业务负责人手机号" />
      </el-form-item>
      <el-form-item label="评价业务联系人" prop="evaluationContact">
        <el-input v-model="formData.evaluationContact" placeholder="请输入评价业务联系人" />
      </el-form-item>
      <el-form-item label="评价业务联系人手机号" prop="evaluationContactPhone">
        <el-input v-model="formData.evaluationContactPhone" placeholder="请输入评价业务联系人手机号" />
      </el-form-item>
      <el-form-item label="对外公布联系人" prop="publicContact">
        <el-input v-model="formData.publicContact" placeholder="请输入对外公布联系人" />
      </el-form-item>
      <el-form-item label="对外公布联系人手机号" prop="publicContactPhone">
        <el-input v-model="formData.publicContactPhone" placeholder="请输入对外公布联系人手机号" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { FilingCreationApi, FilingCreation } from '@/api/eval/filingcreation'

/** 创建备案 表单 */
defineOptions({ name: 'FilingCreationForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  enterpriseName: undefined,
  registeredAddress: undefined,
  businessScope: undefined,
  unifiedSocialCreditCode: undefined,
  legalRepresentative: undefined,
  registrationAuthority: undefined,
  institutionNature: undefined,
  soeType: undefined,
  applicationName: undefined,
  mohrssFilingNumber: undefined,
  headOfficeName: undefined,
  lastYearEmployeeCount: undefined,
  lastYearEducationFund: undefined,
  employeeCount: undefined,
  registeredCapital: undefined,
  lastYearRevenue: undefined,
  evaluationBusinessLeader: undefined,
  evaluationBusinessLeaderPhone: undefined,
  evaluationContact: undefined,
  evaluationContactPhone: undefined,
  publicContact: undefined,
  publicContactPhone: undefined
})
const formRules = reactive({
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await FilingCreationApi.getFilingCreation(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as FilingCreation
    if (formType.value === 'create') {
      await FilingCreationApi.createFilingCreation(data)
      message.success(t('common.createSuccess'))
    } else {
      await FilingCreationApi.updateFilingCreation(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    enterpriseName: undefined,
    registeredAddress: undefined,
    businessScope: undefined,
    unifiedSocialCreditCode: undefined,
    legalRepresentative: undefined,
    registrationAuthority: undefined,
    institutionNature: undefined,
    soeType: undefined,
    applicationName: undefined,
    mohrssFilingNumber: undefined,
    headOfficeName: undefined,
    lastYearEmployeeCount: undefined,
    lastYearEducationFund: undefined,
    employeeCount: undefined,
    registeredCapital: undefined,
    lastYearRevenue: undefined,
    evaluationBusinessLeader: undefined,
    evaluationBusinessLeaderPhone: undefined,
    evaluationContact: undefined,
    evaluationContactPhone: undefined,
    publicContact: undefined,
    publicContactPhone: undefined
  }
  formRef.value?.resetFields()
}
</script>